BUI.use(['bui/form','bui/overlay','bui/data','bui/select','bui/slider','bui/tab','bui/calendar'],function(Form,Overlay,Data,Select,Slider,Tab,Calendar){ 
		var dialoggft = new Overlay.Dialog({
            title:'添加奖品',
            width:600,
            height:"auto",
            //配置DOM容器的编号
            contentId:'content',
			success:function () {
				var action = $(".action").val();
				var egid = $(".egid").val();
				var last_quant = $(".last_quant").val();
				var gift_name = $(".gift-name").val();
				var gift_id = $(".gift-id").val();
				var gift_type = $(".gift-type").val();
				var pic_url = $(".js-gift_pic").val();
				var quantity = $(".js-quantity").val();
				var probability = $(".js-probability").val();
				var per_user = $(".js-per_user").val();
				var advance = $(".advance").val();
				var every_minute = $(".js-every-minute").val();
				var range_time_start = $(".range_time_start").val();
				var range_time_end = $(".range_time_end").val();
				var range_probability = $(".js-time-range-prob").val();
				var every_lotts = $(".js-every-lotts").val();
				var advance_quantity = $(".js-advance_quantity").val();
				var ext_rule = $(".js-ext-rule").val();
				var medal_id = $(".js-medalid").val();
				var medal_name = $(".js-medal_name").val();
				var medal_pic = $(".js-medal_pic").val();
				var rule_medalid = $(".js-rule_medalid").val();
				var rule_medal_name = $(".js-rule_medal_name").val();
				var rule_medal_pic = $(".js-rule_medal_pic").val();
				var medal_quant = $(".js-medal_quant").val();
				var show_title = $(".js-show-title").val();
				var show_content = $(".js-show-content").val();
				var jifen = $(".js-jifen").val();
				var payment = $(".js-payment").val();
				var pay_time_start = $(".pay_time_start").val();
				var pay_time_end = $(".pay_time_end").val();
				var level_num = $(".js-level_num").val();
				var level_name = $(".js-level_name").val();
				var collect_items = $(".collect_items").val();
				var order_items = $(".order_items").val();
				var cart_items = $(".cart_items").val();
				var passwd = $(".js-passwd").val();
				$.ajax(
				{"url":"/?m=shopmanage&c=eventset&a=set_event_gift",					
					"dataType": "json",
					"type": "post",
					"data":{
							"egid":egid,
							"action":action,
							"last_quant":last_quant,
							"gift_name":gift_name,
							"gift_id":gift_id,
							"level_num":level_num,
							"level_name":level_name,
							"gift_type":gift_type,
							"pic_url":pic_url,
							"quantity":quantity,
							"probability":probability,
							"per_user":per_user,
							"advance":advance,
							"every_minute":every_minute,
							"range_time_start":range_time_start,
							"range_time_end":range_time_end,
							"range_probability":range_probability,
							"every_lotts":every_lotts,
							"advance_quantity":advance_quantity,
							"ext_rule":ext_rule,
							"medal_id":medal_id,
							"medal_name":medal_name,
							"medal_pic":medal_pic,
							"medal_quant":medal_quant,
							"rule_medalid":rule_medalid,
							"rule_medal_name":rule_medal_name,
							"rule_medal_pic":rule_medal_pic,
							"collect_items":collect_items,
							"order_items":order_items,
							"cart_items":cart_items,
							"show_title":show_title,
							"show_content":show_content,
							"jifen":jifen,
							"passwd":passwd,
							"payment":payment,
							"pay_time_start":pay_time_start,
							"pay_time_end":pay_time_end
							},
					"success":function(data,status){
						if(data.hasError){
							alert(data.error);
						}
						// alert("已成功添加！");
						if(!$(".eid").val()){
							location.href="/?m=shopmanage&c=event&a=step3";
						}else{
							location.href="/?m=shopmanage&c=Edit&a=gifts&eid="+$(".eid").val();
						}
					}
				});
				
			}
		});
	$(".js-cancel-metal").on("click",function(){
		$(".js-rule_medalid").val("");
		$(".js-rule_medal_name").val("");
		$(".js-rule_medal_pic").val("");
		$(".js-rule_medal").removeClass("hide");
		$(this).addClass("hide");
		$(".rule_medal").empty();
	});
	$(".js-addgift").on("click",function(){
		$(".action").val("add");
		$(".advance").val("no");
		$(".js-set-advance").hide();
		$(".js-ext").hide();
		extrule.setSelectedValue("");
		rangeprob.slideTo(0);
		prob_slider.slideTo(0);
		prob_slider.set('max',parseInt(left_prob));
		dialoggft.show();
	});
	var giftstore = new Data.Store({  //自动读取后台奖品数据
      url : '/?m=Kxdr&c=Gameset&a=get_gifts',
      autoLoad : true,
    });
	// 拖动选取奖品概率的控件
	var left_prob = $(".left_prob").val();

	 var prob_slider = new Slider.Slider({
        render : '#sl1',
        max : left_prob, //最大值
        value : 0 //当前值
      });
      prob_slider.render();
      prob_slider.on('change',function(ev){
        $('.js-probability').val(ev.value/100000);
      });
	// 拖动选取奖品概率的控件
	 var rangeprob = new Slider.Slider({
        render : '#time-range-prob',
        max : 10000000, //最大值
        value : 0 //当前值
      });
      rangeprob.render();
      rangeprob.on('change',function(ev){
        $('.js-time-range-prob').val(ev.value/100000);
      });
	//选择可获取奖品的控件
    sysgift = new Select.Select({  
		  elCls : 'pull-left',
          render:'#s5',
          valueField:'.js-hqjpval',
          store:giftstore
        });
    sysgift.render();
	sysgift.on("change",function(ev){
		$(".pic-row").css({"height":"110px"});
		$(".gift-name").val(ev.item.name);
		$(".gift-id").val(ev.item.id);
		$(".gift-type").val(ev.item.type);
		$(".js-gift_pic").val(ev.item.pic_url);
		$(".gift-pic").attr("src",ev.item.pic_url+"_100x100");
		gift_quant.slideTo(ev.item.quant_init?ev.item.quant_int:0);
		gift_quant.set('max',ev.item.quantity>=0?ev.item.quantity:0);
		gift_quant.render();
	});
	  // 拖动选择奖品数量的控件
	  var gift_quant = new Slider.Slider({
        render : '#sl2',
        max : 1, //最大值
        value : 0 //当前值
      });
      gift_quant.render();
      gift_quant.on('change',function(ev){
        $('.js-quantity').val(ev.value);
		advance_quant.slideTo(0);
		advance_quant.set('max',ev.value>=0?ev.value:0);
		advance_quant.render();
      });	  
	  // 高级数量
	  var advance_quant = new Slider.Slider({
        render : '#sl3',
        max : 1, //最大值
        value : 0 //当前值
      });
      advance_quant.render();
      advance_quant.on('change',function(ev){
        $('.js-advance_quantity').val(ev.value);
      });
	  var items5 = [
          {text:'无附加条件',value:""},
          {text:'订单兑奖',value:'order'},
          {text:'积分兑奖',value:'jifen'},
          {text:'勋章兑奖',value:'metal'},
          // {text:'收藏单品',value:'collect'},
          // {text:'加购物车',value:'addcart'},
          {text:'炫耀得奖',value:'friend_show'},
          {text:'凭密码领奖',value:'password'},
          // {text:'晒单得奖',value:'6'},
          // {text:'互动得奖',value:'4'},
          // {text:'加购物车得奖',value:'4'},
          // {text:'回帖得奖',value:'7'},
        ],
	levels = [
          {text:'一等奖',value:'1',level:1},
          {text:'二等奖',value:'2',level:2},
          {text:'三等奖',value:'3',level:3},
          {text:'输入等级',value:'input',level:"",name_input:""},
          {text:'不显示等级',value:'hide',level:0},
          {text:'隐含奖品',value:'implicat',level:'implicat'}
        ],
    level_select = new Select.Select({  
          render:'.select_level',
          items:levels
        });
    level_select.render();
    level_select.on('change', function(ev){
		$(".js-level_name").val(ev.item.text);
		$(".js-level_num").val(ev.item.level);
		if(ev.item.value=="input"){
			// $(".js-level_num").val("");
			$(".js-level_name").val(ev.item.name_input);
			$(".js-set-level_name").show();
		}else{
			$(".js-set-level_name").hide();
		}
		if(ev.item.value=="hide"){$(".js-level_name").val("");}
    });
	// 选择获取方式的控件
    extrule = new Select.Select({  
          render:'#s6',
          valueField:'.js-ext-rule',
          items:items5
        });
	// extrule.setSelectedValue(0);
    extrule.render();
	extrule.on('change', function(ev){
		$(".js-ext").hide();
		switch(ev.item.value){
			case "jifen":
			$(".js-set-jifen").show();
			break;
			case "order":
			$(".js-set-order").show();
			break;
			case "metal":
			$(".js-set-metal").show();
			break;
			case "collect":
			$(".js-set-item").show();
			break;
			case "addcart":
			$(".js-set-cart").show();
			break;
			case "friend_show":
			$(".js-set-show-firend").show();
			break;
			case "password":
			$(".js-set-passwd").show();
			break;
		}
    });
	var datepicker = new Calendar.DatePicker({
	  trigger:'.calendar',
	  showTime:false,
	  autoRender : true
	});
	var datepicker = new Calendar.DatePicker({
	  trigger:'.calendar-time',
	  showTime:true,
	  autoRender : true
	});
	$(".js-advance").on("click",function(){
		var tp = $(this).attr("data-advance");
		$(".advance").val(tp);
		$(".js-set-advance").hide();
		switch(tp){
			case "time-range":
			$(".js-set-time-range").show();
			$(".js-set-etc-quantity").show();
			break;
			case "ev-minute":
			$(".js-set-ev-minute").show();
			$(".js-set-etc-quantity").show();
			break;
			case "ev-lotts":
			$(".js-set-ev-lotts").show();
			$(".js-set-etc-quantity").show();
			break;
		}
	});
	$(".js-edit").on("click",function(){
		var id = $(this).attr("data-id");
		var fid = $(this).attr("data-fid");
		var ac = $(this).attr("data-ac");
		var quant = $(this).attr("data-quant");
		if(ac=="delete"){
			BUI.Message.Show({
					"msg" : "确实要删除这个奖品吗？",
					"icon" : 'question',
					"mask":false,
					"buttons" : [
					  {
						text:'删除',
						elCls : 'button button-mini button-danger ',
						handler : function(){
						  var $this = this;
						  $.ajax(
							{"url":"/?m=shopmanage&c=eventset&a=del_event_gift",					
								"data": {"id":id,"fid":fid,"quant":quant},
								"dataType": "json",
								"success":function(data,status){
									if(!data.hasError){
										$(".gift"+id).remove();
										liuxiaofan();
										giftstore.load();
										$this.close();
									}else{
										alert("出错！"+data.error);
									}
								}
							});
						}
					  },{
						text:'取消',
						elCls : 'button button-mini ',
						handler : function(){
						  this.close();
						}
					  }]
			});
			
		}
		if(ac=="edit"){
			$.ajax(
			{"url":"/?m=shopmanage&c=eventset&a=get_event_gift",	
				"data":{"id":id},		
				"dataType": "json",
				"success":function(data,status){
					$(".action").val("edit");
					$(".egid").val(id);
					$(".last_quant").val(data.quantity);
					sysgift.setSelectedValue(data.gift_id);
					$(".js-quantity").val(data.quantity);
					gift_quant.slideTo(parseInt(data.quantity));
					$(".js-probability").val(data.probability_show);
					prob_slider.set('max',parseInt(data.probability)+parseInt(left_prob));
					prob_slider.slideTo(parseInt(data.probability));
					$(".js-ext").hide();
					extrule.setSelectedValue("");
					extrule.setSelectedValue(data.ext_rule);
					$(".advance").val(data.advance);
					$(".js-set-advance").hide();
					$(".js-set-etc-quantity").hide();
					$(".js-set-"+data.advance).show();
					if(data.advance!="no"){
					$(".js-set-etc-quantity").show();
					}
					$(".collect_items").val(data.collect_items);
					$(".items-collect_items").text(data.collect_items_count);
					$(".cart_items").val(data.cart_items);
					$(".items-cart_items").text(data.cart_items_count);
					$(".range_time_start").val(data.range_time_start);
					$(".range_time_end").val(data.range_time_end);
					rangeprob.slideTo(parseInt(data.advance_probability));
					advance_quant.slideTo(parseInt(data.advance_quantity));
					$(".js-every-minute").val(data.every_minute);
					$(".js-every-lotts").val(data.every_lotts);
					$(".js-payment").val(data.payment);
					$(".pay_time_start").val(data.pay_time_start);
					$(".pay_time_end").val(data.pay_time_end);
					$(".order_items").val(data.order_items);
					$(".items-order_items").text(data.order_items_count);
					if(data.rule_medal_id>0){
						$(".js-cancel-metal").removeClass("hide");
						$(".js-get-metal").addClass("hide");
						$(".js-rule_medalid").val(data.rule_medal_id);
						$(".js-rule_medal_name").val(data.rule_medal_name);
						$(".js-rule_medal_pic").val(data.rule_medal_pic);
						$(".rule_medal").html("<span><img src=\""+data.rule_medal_pic+"_20x20\" \>"+data.rule_medal_name+"</span>");
					}else{
						$(".js-rule_medalid").val("");
						$(".js-rule_medal_name").val("");
						$(".js-rule_medal_pic").val("");
						$(".rule_medal").empty();
						$(".js-cancel-metal").addClass("hide");
						$(".js-get-metal").removeClass("hide");
					}
					if(data.medal_id>0){
						$(".js-medalid").val(data.medal_id);
						$(".js-medal_name").val(data.medal_name);
						$(".js-medal_pic").val(data.medal_pic);
						$(".js-set-metal").removeClass("hide");
						$(".medal").html("<span><img src=\""+data.medal_pic+"_20x20\" \>"+data.medal_name+"</span>");
						$(".js-medal_quant").val(data.medal_quant);
					}else{
						$(".js-medalid").val("");
						$(".js-medal_name").val("");
						$(".js-medal_pic").val("");
						$(".js-set-metal").addClass("hide");
						$(".medal").empty();
						$(".js-medal_quant").val(0);
					}
					// $(".js-time-range-prob").val(data.advance_probability);
					$(".js-jifen").val(data.jifen);
					$(".js-per_user").val(data.per_user);
					$(".js-level_num").val(data.level);
					$(".js-level_name").val(data.level_name);
					if(data.status==1){
						$(".js-level_num").val("implicat");
						$(".js-level_name").val("隐含奖品");
						level_select.setSelectedValue("implicat");
					}else{
						if(data.level>3){
							levels[3].level=data.level;
							levels[3].name_input=data.level_name;
							level_select.setSelectedValue("input");
						}else{
							level_select.setSelectedValue(data.level);
						}
					}
					if(data.level==0){
						$(".js-level_num").val(0);
						$(".js-level_name").val("");
					}
					if(data.status!=1 && data.level>3){
						$(".js-set-level_name").show();
					}
					dialoggft.show();
				}
			});
		}
	});
	$(".js-get-metal").on("click",function(){
		var tp = $(this).attr("data-tp")?$(this).attr("data-tp")+"_":'';
		$.ajax(
		{"url":"/?m=Kxdr&c=medal&a=get_medal_as_gift",	
			"data":{"tp":tp},		
			"dataType": "json",
			"success":function(data,status){
				var msg="";
				if(data.hasError){
					msg="<h2>出错！</h2><br />"+data.error;
				}else{
					msg="<h2>请选择勋章：</h2><br /><div style=\"height:300px;overflow-y:scroll;\">";
					for(var i=0;i<data.data.length;i++){
						msg+='<li><span class="md'+data.data[i].id+'"><span> <img src="'+data.data[i].pic_url+'_20x20.jpg" />  '+data.data[i].title+'</span></span><button class="button button-mini umps" data-id="'+data.data[i].id+'" data-pic="'+data.data[i].pic_url+'" data-name="'+data.data[i].title+'" type="button">选择</button></li>';
					}
					msg+="</div>";
				}
				BUI.Message.Show({
					"msg" : msg,
					"icon" : 'question',
					"mask":false,
					"buttons" : [
					  {
						text:'确认',
						elCls : 'button button-mini js-msg',
						handler : function(){
						  this.close();
						}
					  },{
						text:'添加奖章',
						elCls : 'button button-mini',
						handler : function(){
							BUI.Message.Show({
								"msg" : "<h1>进入添加勋章</h1><br  /><div class=\"centered\"><a href=\"/?m=Kxdr&c=Medal\" target=\"_blank\" class=\"button button-big\">添加勋章</a></div>",
								"icon" : 'question',
								"mask":false,
								"buttons" : [
								  
								]
							});
						}
					  }
					]
				});
				$(".umps").on("click",function(){
					$(".umps").removeClass("button-danger");
					$(this).addClass("button-danger");
					$(".js-giftname").val($(this).attr("data-name"));
					$(".js-"+tp+"medalid").val($(this).attr("data-id"));
					$(".js-"+tp+"medal_pic").val($(this).attr("data-pic"));
					$(".js-"+tp+"medal_name").val($(this).attr("data-name"));
					$("."+tp+"medal").empty();
					$($(".md"+$(this).attr("data-id")).html()).appendTo("."+tp+"medal");
					$(".js-"+tp+"medal_c").removeClass("hide");
					$(".js-"+tp+"medal").addClass("hide");
					var html = "直接出现在勋章列表";
					$(".js-detail").val(html);
					$(".js-msg").click();
					$(".js-giftname").blur();
				});
			}
		});
	});
	$(".js-itemset").on("click",function(){
		getitems("","","","collect_items",10000);
	});
	$(".js-cartset").on("click",function(){
		getitems("","","","cart_items",10000);
	});
	$(".js-orderset").on("click",function(){
		getitems("","","","order_items",10000);
	});
	$(".js-cartset-preview").on("click",function(){
		var iids = $(this).attr("data-items");
			$(".preview").val(iids);
			iids = JSON.parse(decodeURI(iids));
		var numiids = new Array();
			for(var i=0;i<iids.length;i++){
				numiids.push(iids[i][0]);
			}
		getitems("","",numiids.join(","),"preview",10000);
	});
	$(".js-collect-preview").on("click",function(){
		var iids = $(this).attr("data-items");
			$(".preview").val(iids);
			iids = JSON.parse(decodeURI(iids));
		var numiids = new Array();
			for(var i=0;i<iids.length;i++){
				numiids.push(iids[i][0]);
			}
		getitems("","",numiids.join(","),"preview",10000);
	});
	$(".js-random").on("click",function(){
		$(".js-passwd").val(Math.floor(100+Math.random()*(999-100)));
	});
	function showDialog(title,msg,input){
		BUI.Message.Show({
			"msg" : "<h3>"+title+"</h3>"+msg,
			"icon" : 'question',
			"mask":false,
			"buttons" : [
			  {
				text:'确认',
				elCls : 'button button-mini js-close',
				handler : function(){
					this.close();
				}
			  }
			]
		});
	}
	function closeDialog(title,msg,input){
		$(".js-close").click();
	}
	/*
	 * q: 搜索关键字 
	 * pg:第几页 
	 * input:保存的选择数据 
	 * numiids:已设置的iids
	 * limit:限制做多选择的个数
	 */
	function getitems(q,pg,numiids,input,limit){
		Q=q?q:"";
		PG=pg?pg:1;
		limit=limit?limit:1;
		input=input?input:"order_items";
		showDialog("正在读取数据。。。","<img src=\"http://gtd.alicdn.com/tps/i3/T1b1m3XkVpXXXXXXXX-32-32.gif\" />","form-horizontal");	
		var item_iids = $('.'+input).val();
		if(item_iids!=""){
			num_iids = JSON.parse(decodeURI(item_iids));
		// num_iids = item_iids.split(",");
		}else{
		num_iids=new Array();
		}
		$.ajax({
			"url": "/?a=items&m=shopmanage&c=Service",
			"data":{"q":Q,"pg":PG,"numiids":numiids},
			"dataType": "json",
			"success": function(data, textStatus) {
			closeDialog();
			if(data.code){
				showDialog("读取失败！",data.msg+data.sub_msg,"form-horizontal");
				return;
			}
			if(data.total_results==0){
				showDialog("读取失败！","没有读取到任何商品哦！","form-horizontal");
			}else{
				var item="<input type=\"text\" value=\""+data.q+"\" class=\"js-gjc\" /><button type=\"button\" class=\"button button-mini js-so\">搜索</button>  共"+data.total_results+"件商品  <button type=\"button\" class=\"button button-mini button-success js-ready\" data-input=\""+input+"\">查看已设置的商品</button>";
				if(input!="preview"){
				item+="<button type=\"button\" class=\"button button-mini js-ready-all\">查看全部</button> <!--button type=\"button\" class=\"button button-mini js-select-all\">全选本页</button-->";
				}
				item +=("<hr style=\"clear:both\">");
				for(var i=0;i<data.items.item.length;i++){
				item +=("<table style=\"width:140px;float:left;margin-right:5px\" cellspacing=\"0\" class=\"table table-bordered table-item\">");
				item +=("	<tbody>");
				item +=("	<tr><td>");
				item +=("		<a href=\"http://item.taobao.com/item.htm?id="+data.items.item[i].num_iid+"\" target=\"_blank\" ><img src=\""+data.items.item[i].pic_url+"_120x120.jpg\"></a>");
				// item +=("	</td></tr><tr><td>");
				item +=("	商品标题："+decodeURI(data.items.item[i].title));
				var css = "";
				var text = "选择";
				for (var j=0;j<num_iids.length;j++){
					if(data.items.item[i].num_iid==num_iids[j][0]){
						css = "button-danger";
						text = "取消选择";
						break;
					}
				}
				if(input!="preview"){
				item +=("	<div style=\"text-align:center\"><button type=\"button\" class=\"button button-small "+css+" js-select\" data-price=\""+data.items.item[i].price+"\" data-title=\""+decodeURI(data.items.item[i].title)+"\" data-pic=\""+data.items.item[i].pic_url+"\" data-iid=\""+data.items.item[i].num_iid+"\">"+text+"</button></div>");
				}
				item +=("	</td></tr>");
				item +=("	</tbody>");
				item +=("</table>");
				}
				item +=("<hr style=\"clear:both\">");
				item +=("<div class=\"centered\"><button type=\"button\" class=\"button button-mini js-pgup\">上一页</button>");
				item +=("<button type=\"button\" class=\"button button-mini js-pgdown\">下一页</button></div>");
				showDialog("请选择商品",item,"form-horizontal");
				$(".js-ready-all").on("click",function(){
					getitems("","","",$(this).attr("data-input"),limit);
				});
				$(".js-select-all").on("click",function(){
					$(".js-select").fire("click");
				});
				$(".js-select").on("click",function(){
					if(num_iids.length>=limit){
						alert("只能选择"+limit+"件商品！");
						return;
					}
					var key = $(this).attr("data-iid");
					var pic = $(this).attr("data-pic");
					var title = $(this).attr("data-title");
					var price = $(this).attr("data-price");
					var item_json = new Array(key,pic,title,price);
					if($(this).hasClass("button-danger")){
						for(var i=0;i<num_iids.length;i++){
							if(num_iids[i][0]==key){
								num_iids.splice(i,1);
							}
						}
						$("."+input).val(encodeURI(JSON.stringify(num_iids)));
						$(this).removeClass("button-danger");
						$(this).text("选择");
						$(".items-"+input).text(num_iids.length);
					}else{
						num_iids.push(item_json);
						$("."+input).val(encodeURI(JSON.stringify(num_iids)));
						$(this).addClass("button-danger");
						$(this).text("取消选择");
						$(".items-"+input).text(num_iids.length);
					}
				});
				$(".js-pgup").on("click",function(){
					if(PG==1){
						alert("没有上一页啦！");
					}else{
						PG--;
						getitems($(".js-gjc").val(),PG,numiids,input,limit);
					}
				});
				$(".js-pgdown").on("click",function(){
					if(PG>(data.total_results/20)){
						alert("没有下一页啦！");
					}else{
						PG++
						getitems($(".js-gjc").val(),PG,numiids,input,limit);
					}
				});
				$(".js-ready").on("click",function(){
					var item_iids = $('.'+input).val();
					if(!item_iids){
					alert("您还没有选择任何商品！");
					}else{
					getitems("","",item_iids,input,limit);
					}
				});
			}
			$(".js-so").on("click",function(){
				getitems($(".js-gjc").val(),"","",input,limit);
			});
			},
			"complete": function(data){
				
			},
			"error": function(){
				alert('error');
			}
		});
	}
	
	function liuxiaofan(){
		var margin = 20;//设置间距
		var li=$(".breadcrumb");//区块名称
		if(li.length<=0){
			return;
		}
		var li_W = li[0].offsetWidth+margin;//取区块的实际宽度
		var h=[];//记录区块高度的数组
		var n = 960/li_W|0;
		for(var i = 0;i < li.length;i++) {
			li_H = li[i].offsetHeight;//获取每个li的高度
			if(i < n) {//n是一行最多的li，所以小于n就是第一行了
				max_H =Math.max.apply(null,h);
				h[i]=li_H;//把每个li放到数组里面
				li.eq(i).css("top",0);//第一行的Li的top值为0
				li.eq(i).css("left",i * li_W);//第i个li的左坐标就是i*li的宽度
				}
			else{
				min_H =Math.min.apply(null,h) ;//取得数组中的最小值，区块中高度值最小的那个
				minKey = getarraykey(h, min_H);//最小的值对应的指针
				h[minKey] += li_H+margin ;//加上新高度后更新高度值
				li.eq(i).css("top",min_H+margin);//先得到高度最小的Li，然后把接下来的li放到它的下面
				li.eq(i).css("left",minKey * li_W);    //第i个li的左坐标就是i*li的宽度
			}
		  //  $("p").eq(i).text("高度："+li_H);//把区块高度值写入对应的区块H2标题里面
		}
		max =Math.max.apply(null,h) ;
		$(".gifts").css("height",max);
	}
	/* 使用for in运算返回数组中某一值的对应项数(比如算出最小的高度值是数组里面的第几个) */
	function getarraykey(s, v) {for(k in s) {if(s[k] == v) {return k;}}}
	liuxiaofan();
});